package com.tanhua.server.serivce;

import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.LikeLoveVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.AnnouncementsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.server.interceptor.UserHolder;

import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 大大大脸怪吖
 * @Date: Created in 20:59 2021/3/14
 */
@Service
public class LikeLoveService {

        @Reference
        private CommentApi commentApi;

        @Reference
        private UserInfoApi userInfoApi;

        @Reference
        private AnnouncementsApi announcementsApi;


    /**
     * 喜欢列表
     */
    public ResponseEntity<Object> queryLoveList(Integer page, Integer pagesize,Integer commentType ) {
        //获取用户Id
        Long userId = UserHolder.getUserId();
        //获取分页数据
        PageResult pageResult = commentApi.queryLoveList(page,pagesize,userId,commentType);
        this.SmeLoveLike(pageResult);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 点赞列表
     */
    public ResponseEntity<Object> querylikeList(Integer page, Integer pagesize, Integer commentType) {
        //获取用户Id
        Long userId = UserHolder.getUserId();
        //获取分页数据
       PageResult pageResult = commentApi.querylikeList(page,pagesize,userId, commentType);

       this.SmeLoveLike(pageResult);
       return ResponseEntity.ok(pageResult);
    }

    /**
     * 方法抽取
     */
    public void SmeLoveLike(PageResult pageResult){
        List<Comment> commentList = (List<Comment>) pageResult.getItems();
        //封装Vo
        List<LikeLoveVo> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(commentList)){
            voList = commentList.stream().map(comment -> {
                LikeLoveVo vo = new LikeLoveVo();
                UserInfo userInfo = userInfoApi.findById(comment.getUserId());
                if (userInfo != null){
                    BeanUtils.copyProperties(userInfo,vo);
                }
                vo.setId(comment.getId().toHexString());
                vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setItems(voList);

    }

    /**
     *评论列表
     */
    public ResponseEntity<Object> querycommentsList(Integer page, Integer pagesize, Integer commentType) {
        //获取id
        Long userId = UserHolder.getUserId();
        PageResult pageResult = commentApi.querycommentsList(page,pagesize,userId,commentType);

        this.SmeLoveLike(pageResult);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：公告列表
     */
    public ResponseEntity<Object> queryAnnouncement(Integer page, Integer pagesize) {
        PageResult pageResult = announcementsApi.queryAnnouncements(page,pagesize);

        List<Announcement> announcementList= (List<Announcement>) pageResult.getItems();

        List<AnnouncementVo> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(announcementList)){
            voList = announcementList.stream()
                    .filter(announcement -> announcement.getId()!=null)
                    .map(announcement -> {
                AnnouncementVo  vo = new AnnouncementVo();
               Announcement annId = announcementsApi.findById(announcement.getId());
                BeanUtils.copyProperties(announcement,vo);
                vo.setCreateDate(new DateTime(announcement.getCreated()).toString("yyyy年MM月dd HH:mm:ss"));
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setItems(voList);
        return ResponseEntity.ok(pageResult);
    }
}